<template>
  <div class="app-container">
    <div class="head-container">
      <!-- 搜索 -->
      <el-input v-model="listQuery.Filter" clearable size="small" placeholder="搜索..." style="width: 200px;"
        class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search"
        @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus"
        @click="handleCreate">新增</el-button>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-edit"
        @click="handleUpdate()">修改</el-button>
      <el-button slot="reference" class="filter-item" type="danger" icon="el-icon-delete" size="mini"
        @click="handleDelete()">删除</el-button>
    </div>
    <el-dialog :visible.sync="dialogFormVisible" :close-on-click-modal="false" @close="cancel()"
      :title="formTitle">
      <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="采购编号" prop="purchaseNumber">
          <el-input v-model="form.purchaseNumber" placeholder="请输入采购编号" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="单据编号" prop="documentNumber">
          <el-input v-model="form.documentNumber" placeholder="请输入单据编号" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="申请时间" prop="applicationTimeline">
          <el-input v-model="form.applicationTimeline" placeholder="请输入申请时间" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="提醒" prop="remind">
          <el-input v-model="form.remind" placeholder="请输入提醒" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="交接状态" prop="handoverStatus">
          <el-input v-model="form.handoverStatus" placeholder="请输入交接状态" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="入库状态" prop="inboundStatus">
          <el-input v-model="form.inboundStatus" placeholder="请输入入库状态" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="库位" prop="binlocationId">
          <el-input v-model="form.binlocationId" placeholder="请输入库位" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="库位" prop="field118">
          <el-input v-model="form.field118" placeholder="请输入库位" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="区域" prop="regionId">
          <el-input v-model="form.regionId" placeholder="请输入区域" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="预约时间" prop="makeAppointment">
          <el-input v-model="form.makeAppointment" placeholder="请输入预约时间" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="库区" prop="reservoirAreaId">
          <el-input v-model="form.reservoirAreaId" placeholder="请输入库区" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="单据类型" prop="documenttype">
          <el-input v-model="form.documenttype" placeholder="请输入单据类型" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="交接时间" prop="handoverTime">
          <el-input v-model="form.handoverTime" placeholder="请输入交接时间" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="运单号" prop="trackingNumber">
          <el-input v-model="form.trackingNumber" placeholder="请输入运单号" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="外部单号" prop="externalNumber">
          <el-input v-model="form.externalNumber" placeholder="请输入外部单号" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="仓库" prop="repositoryId">
          <el-input v-model="form.repositoryId" placeholder="请输入仓库" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="申请人" prop="applicant">
          <el-input v-model="form.applicant" placeholder="请输入申请人" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="单据备注" prop="documentRemarks">
          <el-input v-model="form.documentRemarks" placeholder="请输入单据备注" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="承运商" prop="carriers">
          <el-input v-model="form.carriers" placeholder="请输入承运商" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button size="small" type="text" @click="cancel">取消</el-button>
        <el-button size="small" v-loading="formLoading" type="primary" @click="save">确认</el-button>
      </div>
    </el-dialog>
    <el-table ref="multipleTable" v-loading="listLoading" :data="list" size="small" style="width: 90%;"
      @sort-change="sortChange" @selection-change="handleSelectionChange" @row-click="handleRowClick">
      <el-table-column type="selection" width="44px"></el-table-column>
      <el-table-column label="采购编号" prop="purchaseNumber" align="center" />
      <el-table-column label="单据编号" prop="documentNumber" align="center" />
      <el-table-column label="申请时间" prop="applicationTimeline" align="center" />
      <el-table-column label="提醒" prop="remind" align="center" />
      <el-table-column label="交接状态" prop="handoverStatus" align="center" />
      <el-table-column label="入库状态" prop="inboundStatus" align="center" />
      <el-table-column label="库位" prop="binlocationId" align="center" />
      <el-table-column label="库位" prop="field118" align="center" />
      <el-table-column label="区域" prop="regionId" align="center" />
      <el-table-column label="预约时间" prop="makeAppointment" align="center" />
      <el-table-column label="库区" prop="reservoirAreaId" align="center" />
      <el-table-column label="单据类型" prop="documenttype" align="center" />
      <el-table-column label="交接时间" prop="handoverTime" align="center" />
      <el-table-column label="运单号" prop="trackingNumber" align="center" />
      <el-table-column label="外部单号" prop="externalNumber" align="center" />
      <el-table-column label="仓库" prop="repositoryId" align="center" />
      <el-table-column label="申请人" prop="applicant" align="center" />
      <el-table-column label="单据备注" prop="documentRemarks" align="center" />
      <el-table-column label="承运商" prop="carriers" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)" icon="el-icon-edit" />
          <el-button type="danger" size="mini" @click="handleDelete(row)" icon="el-icon-delete" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="totalCount>0" :total="totalCount" :page.sync="page"
      :limit.sync="listQuery.MaxResultCount" @pagination="getList" />
  </div>
</template>
<script>
import Pagination from "@/components/Pagination";
import permission from "@/directive/permission/index.js";
const defaultForm = {
  id: null,
  purchaseNumber: null,
  documentNumber: null,
  applicationTimeline: null,
  remind: null,
  handoverStatus: null,
  inboundStatus: null,
  binlocationId: null,
  field118: null,
  regionId: null,
  makeAppointment: null,
  reservoirAreaId: null,
  documenttype: null,
  handoverTime: null,
  trackingNumber: null,
  externalNumber: null,
  repositoryId: null,
  applicant: null,
  documentRemarks: null,
  carriers: null,
}
export default {
  name: 'InboundAppointments',
  components: {
    Pagination
  },
  directives: {
    permission
  },
  props: [],
  data() {
    return {
      rules: {
        purchaseNumber: [{
          required: true,
          message: '请输入采购编号',
          trigger: 'blur'
        }],
        documentNumber: [{
          required: true,
          message: '请输入单据编号',
          trigger: 'blur'
        }],
        applicationTimeline: [{
          required: true,
          message: '请输入申请时间',
          trigger: 'blur'
        }],
        remind: [{
          required: true,
          message: '请输入提醒',
          trigger: 'blur'
        }],
        handoverStatus: [{
          required: true,
          message: '请输入交接状态',
          trigger: 'blur'
        }],
        inboundStatus: [{
          required: true,
          message: '请输入入库状态',
          trigger: 'blur'
        }],
        binlocationId: [{
          required: true,
          message: '请输入库位',
          trigger: 'blur'
        }],
        field118: [{
          required: true,
          message: '请输入库位',
          trigger: 'blur'
        }],
        regionId: [{
          required: true,
          message: '请输入区域',
          trigger: 'blur'
        }],
        makeAppointment: [{
          required: true,
          message: '请输入预约时间',
          trigger: 'blur'
        }],
        reservoirAreaId: [{
          required: true,
          message: '请输入库区',
          trigger: 'blur'
        }],
        documenttype: [{
          required: true,
          message: '请输入单据类型',
          trigger: 'blur'
        }],
        handoverTime: [{
          required: true,
          message: '请输入交接时间',
          trigger: 'blur'
        }],
        trackingNumber: [{
          required: true,
          message: '请输入运单号',
          trigger: 'blur'
        }],
        externalNumber: [{
          required: true,
          message: '请输入外部单号',
          trigger: 'blur'
        }],
        repositoryId: [{
          required: true,
          message: '请输入仓库',
          trigger: 'blur'
        }],
        applicant: [{
          required: true,
          message: '请输入申请人',
          trigger: 'blur'
        }],
        documentRemarks: [{
          required: true,
          message: '请输入单据备注',
          trigger: 'blur'
        }],
        carriers: [{
          required: true,
          message: '请输入承运商',
          trigger: 'blur'
        }],
      },
      form: Object.assign({}, defaultForm),
      list: null,
      totalCount: 0,
      listLoading: true,
      formLoading: false,
      listQuery: {
        Filter: '',
        Sorting: '',
        SkipCount: 0,
        MaxResultCount: 10
      },
      page: 1,
      dialogFormVisible: false,
      multipleSelection: [],
      formTitle: '',
      isEdit: false,
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    getList() {
      this.listLoading = true;
      this.listQuery.SkipCount = (this.page - 1) * this.listQuery.MaxResultCount;
      this.$axios.gets('/api/business/InboundAppointments', this.listQuery).then(response => {
        this.list = response.items;
        this.totalCount = response.totalCount;
        this.listLoading = false;
      });
    },
    fetchData(id) {
      this.$axios.gets('/api/business/InboundAppointments/' + id).then(response => {
        this.form = response;
      });
    },
    handleFilter() {
      this.page = 1;
      this.getList();
    },
    handleCreate() {
      this.formTitle = '新增入库预约表';
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    handleDelete(row) {
      var params = [];
      let alert = '';
      if (row) {
        params.push(row.id);
        alert = row.name;
      }
      else {
        if (this.multipleSelection.length === 0) {
          this.$message({
            message: '未选择',
            type: 'warning'
          });
          return;
        }
        this.multipleSelection.forEach(element => {
          let id = element.id;
          params.push(id);
        });
        alert = '选中项';
      }
      this.$confirm('是否删除' + alert + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.posts('/api/business/InboundAppointments/delete', params).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          });
          this.getList();
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleUpdate(row) {
      this.formTitle = '修改入库预约表';
      this.isEdit = true;
      if (row) {
        this.fetchData(row.id);
        this.dialogFormVisible = true;
      }
      else {
        if (this.multipleSelection.length != 1) {
          this.$message({
            message: '编辑必须选择单行',
            type: 'warning'
          });
          return;
        }
        else {
          this.fetchData(this.multipleSelection[0].id);
          this.dialogFormVisible = true;
        }
      }
    },
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.formLoading = true;
          this.form.roleNames = this.checkedRole;
          if (this.isEdit) {
            this.$axios.posts('/api/business/InboundAppointments/data-post', this.form).then(response => {
              this.formLoading = false;
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              });
              this.dialogFormVisible = false;
              this.getList();
            }).catch(() => {
              this.formLoading = false;
            });
          }
          else {
            this.$axios.posts('/api/business/InboundAppointments/data-post', this.form).then(response => {
              this.formLoading = false;
              this.$notify({
                title: '成功',
                message: '新增成功',
                type: 'success',
                duration: 2000
              });
              this.dialogFormVisible = false;
              this.getList();
            }).catch(() => {
              this.formLoading = false;
            });
          }
        }
      });
    },
    sortChange(data) {
      const {
        prop,
        order
      } = data;
      if (!prop || !order) {
        this.handleFilter();
        return;
      }
      this.listQuery.Sorting = prop + ' ' + order;
      this.handleFilter();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleRowClick(row, column, event) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    cancel() {
      this.form = Object.assign({}, defaultForm);
      this.dialogFormVisible = false;
      this.$refs.form.clearValidate();
    },
  }
}

</script>
<style>
</style>
